//
//  SideMenuViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-5-11.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "MenuViewController.h"
#import <UIViewController+MMDrawerController.h>
//#import <UIImageView+WebCache.h>
#import "AlbumViewController.h"
#import "NetworkAccessManager.h"
#import "UIAlertView+Kirinrin.h"
#import "ChatViewController.h"
#import "EaseMob.h"

#define kSampleIconImage    @"UserProfileIcon"

@interface MenuViewController (){
    NSOperationQueue *operationQueue;
}
@property (weak, nonatomic) IBOutlet UIImageView *userPhoto;
@property (weak, nonatomic) IBOutlet UILabel *userName;

@property (weak, nonatomic) IBOutlet UILabel *contactMenuTitle;
@property (weak, nonatomic) IBOutlet UIButton *checkOnButton;

@property (nonatomic,copy) NSString *teacherName;

@property (weak, nonatomic) IBOutlet UILabel *restApplicationTitle;

@end

@implementation MenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    
    CALayer *layer = [self.userPhoto layer];
    layer.borderColor = [[UIColor whiteColor] CGColor];
    layer.borderWidth = 2.0f;
    

    [self.userName setText:self.appDelegate.user[@"name"]];
    
    if (self.appDelegate.userType == UserTypeTeacher) {
        [self.contactMenuTitle setText:@"联系家长"];
        [self.checkOnButton setEnabled:YES];
        
        [self.restApplicationTitle setText:@"托咐提醒"];
    }else{
        [self.contactMenuTitle setText:@"老师对话"];
//        [self.checkOnButton setTitle:@"入园离园" forState:UIControlStateNormal];
//        [self.checkOnButton setEnabled:YES];
        
        [self.restApplicationTitle setText:@"托咐提醒"];
    }
    
    if ([[[EaseMob sharedInstance] chatManager]isLoggedIn]) {
        self.chatButton.enabled = YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
    
    self.userPhoto.image = self.appDelegate.userPhoto;
    /*
    operationQueue = [[NSOperationQueue alloc] init];
    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImage) object:nil];
    [operationQueue addOperation:op];
    
    */
}

-(void)loadImage{
    NSURL *photoUrl = [NSURL URLWithString:self.appDelegate.user[@"photo"]];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoUrl]];
    [self.userPhoto setImage:image];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toChatTeacher"]) {
        ChatViewController *chatViewController = [[segue destinationViewController] viewControllers].lastObject;
        NSString *teacherId = self.appDelegate.user[@"teacherId"];
//        [chatViewController setValue:@"18694047801" forKey:@"chatter"];
        [chatViewController setValue:teacherId forKey:@"chatter"];
        [chatViewController setValue:sender forKey:@"chatterPhotoUrlString"];
        chatViewController.navigationItem.title =self.teacherName;
    }
}

- (IBAction)toChatAction:(id)sender {
    if (self.appDelegate.userType == UserTypeParent) {
        NSString *uriString = [NSString stringWithFormat:@"Teacher/%@.json", self.appDelegate.user[@"teacherId"]];
        NSDictionary *parameters = @{@"key": self.appDelegate.accessKey};
        
        [[NetworkAccessManager shareManager]GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
            
            int resultCode = [responseObject[@"result"]intValue];
            if (resultCode == 0) {
                NSLog(@"teacher photo is %@", responseObject[@"data"][@"photo"]);
                NSURL *photoUrl = [NSURL URLWithString:responseObject[@"data"][@"photo"]];
                self.teacherName = responseObject[@"data"][@"name"];
                [self performSegueWithIdentifier:@"toChatTeacher" sender:photoUrl];
            }else{
                [UIAlertView alertWithRemoteError:resultCode];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"opernation: %@\nerror: %@", operation, error);
            [UIAlertView alertWithNetworkError];
        }];
        
    }else{
        [self performSegueWithIdentifier:@"toChatParent" sender:sender];
    }
}

- (IBAction)toRestAction:(id)sender {
    if (self.appDelegate.userType == UserTypeParent) {
        [self performSegueWithIdentifier:@"toRestApplication" sender:sender];
    }else{
        [self performSegueWithIdentifier:@"toRestApplicationList" sender:sender];
    }
}
- (IBAction)toCheckOnAction:(id)sender {
    if (self.appDelegate.userType == UserTypeParent) {
        [self performSegueWithIdentifier:@"toNavCheckOnParent" sender:sender];
    }else{
        [self performSegueWithIdentifier:@"toNavCheckOnTeacher" sender:sender];
    }
    
}

#pragma mark - anctios
- (IBAction)openAlbum:(id)sender {
    
    NSString *uriString = @"Photos.json";
    NSDictionary *parameters = @{@"key": self.appDelegate.accessKey};
    
    [[NetworkAccessManager shareManager]GET:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            
            
            AlbumViewController *albumVC = [[AlbumViewController alloc]init];
            
            
            albumVC.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
            albumVC.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
            albumVC.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
            albumVC.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
            albumVC.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
            albumVC.startOnGrid = YES; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
            browser.wantsFullScreenLayout = YES;
#endif
            //    albumVC.wantsFullScreenLayout = YES; // iOS 5 & 6 only: Decide if you want the photo browser full screen, i.e. whether the status bar is affected (defaults to YES)
            albumVC.delegate = albumVC;
            albumVC.remoteData = responseObject[@"data"];
//            
            UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:albumVC];
            [self.mm_drawerController setCenterViewController:navVC withCloseAnimation:YES completion:nil];
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];
    
    

}
- (IBAction)openScoreView:(id)sender {
    if (self.appDelegate.userType == UserTypeTeacher) {
        [self performSegueWithIdentifier:@"toScoreTeacher" sender:sender];
    }else{
        [self performSegueWithIdentifier:@"toScoreParent" sender:sender];
    }
}

- (IBAction)openMainAction:(id)sender {
    [self performSegueWithIdentifier:@"toMainView" sender:sender];
}

@end
